package org.jxf.gps.jt808.message;

import org.jfk.core.utils.ByteUtils;
import org.jfk.core.utils.I18nUtil;

/**
 *  位置信息汇报
 * @author jxf
 * 2015年1月17日
 */
public class Msg0200  extends MsgBody{
	byte[] _alertFlag = new byte[4];
	byte[] _status = new byte[4];
	byte[] _lat = new byte[4];
	byte[] _lng = new byte[4];
	byte[] _alt = new byte[2];
	byte[] _speed = new byte[2];
	byte[] _direct = new byte[2];
	byte[] _gpsTime = new byte[6];
	AdditionInfo[] info;
	
	double lat;
	double lng;
	float alt;
	float speed;
	int direct;
	String gpsTime;
	double mileage;
	String stateInfoStr;
	String alertInfoStr;
	@Override
	public byte[] toBuff() {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public void fromBuff(byte[] buff) {
		System.arraycopy(buff, 0, _alertFlag, 0, 4);
		System.arraycopy(buff, 4, _status, 0, 4);
		System.arraycopy(buff, 8, _lat, 0, 4);
		System.arraycopy(buff, 12, _lng, 0, 4);
		System.arraycopy(buff, 16, _alt, 0, 2);
		System.arraycopy(buff, 18, _speed, 0, 2);
		System.arraycopy(buff, 20, _direct, 0, 2);
		System.arraycopy(buff, 22, _gpsTime, 0, 6);
		
		lat = ByteUtils.byte4ToInt(_lat)/1000000.0000000;
		lng = ByteUtils.byte4ToInt(_lng)/1000000.0000000;
		alt = ByteUtils.byte2ToInt(_alt);
		speed = ByteUtils.byte2ToInt(_speed)/10L;
		direct = ByteUtils.byte2ToInt(_direct);
		gpsTime = "20"+ByteUtils.byteToHexString(_gpsTime);
		
		alertInfoStr = getAlertInfoStr(ByteUtils.byte4ToInt(_alertFlag));
		stateInfoStr = getStateInfoStr(ByteUtils.byte4ToInt(_status));
	}
	
	public String toString(){
		return "msg(lat = " + Double.valueOf(lat) 
				+ " lng = " + Double.valueOf(lng)
				+ " alt = " + alt
				+ " speed = " + speed
				+ " direct = " + direct
				+ " gpsTime = " + gpsTime
				+")";
	}
	
	/**
	 * 将报警信息转为文字,通过移位法判定开关量
	 * @param b
	 * @return
	 */
	private static String getAlertInfoStr(int b){
		String s = ",";
		if ((b & 1) == 1){
			s = s + I18nUtil.getValueByKey("alert_info.Urgency")+","; 
		}
		if (((b >> 1) & 1) == 1){
			s = s + I18nUtil.getValueByKey("alert_info.OverSpeed")+",";
		}
		if (((b >> 2) & 1) == 1){
			s = s + I18nUtil.getValueByKey("alert_info.Tired")+",";
		}
		if (((b >> 3) & 1) == 1){
			s = s + I18nUtil.getValueByKey("alert_info.DangerPro")+",";
		}
		if (((b >> 4) & 1) == 1){
			s = s + I18nUtil.getValueByKey("alert_info.GpsModeError")+",";
		}
		if (((b >> 5) & 1) == 1){
			s = s + I18nUtil.getValueByKey("alert_info.GpsAntBreak")+",";
		}
		if (((b >> 6) & 1) == 1){
			s = s + I18nUtil.getValueByKey("alert_info.GpsAntShort")+",";
		}
		if (((b >> 7) & 1) == 1){
			s = s + I18nUtil.getValueByKey("alert_info.LowPower")+",";
		}
		if (((b >> 8) & 1) == 1){
			s = s + I18nUtil.getValueByKey("alert_info.NoPower")+",";
		}
		
		if (",".equals(s)){
			s = "";
		}
		return s;
	}
	
	/**
	 * 将状态转为文字
	 * @param b
	 * @return
	 */
	private static String getStateInfoStr(int b){
		String s = "";
		if ((b & 1) == 1){
			s = s + I18nUtil.getValueByKey("state_info.acc_on");
		}else{
			s = s + I18nUtil.getValueByKey("state_info.acc_off");
		}
		
		if (((b >> 1) & 1) == 1){
			s = s + "-"+I18nUtil.getValueByKey("state_info.located");
		}else{
			s = s + "-"+I18nUtil.getValueByKey("state_info.unlocated");
		}
		return s;
	}
	
	class AdditionInfo{
		int infoId;
		int infoLength;
		String infoData;
	}
	
	public static void main(String[] args){
		System.out.println(getStateInfoStr(0x00001000));
	}
}


